/*
 * FontParametersPanelHandler
 * 
 * Version: 1.0: This class is responsible for generating Font Parameters Panel which lets users select Font Parameters
 * as in Tempo wherever applicable.
 * 
 * Date: 12/05/2010
 * 
 * Licensed under Eclipse open source license
 */

package slhs.tempo.scriptGenerator;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import org.mlc.swing.layout.LayoutConstraintsManager;

public class FontParametersPanelHandler extends JPanel implements PanelHandler {

    private static final FontParametersPanelHandler fontParametersPanelHandlerObject = new FontParametersPanelHandler();

    private static final String FONT_PARAMETERS_PANEL_LAYOUT_XML_FILE = "FONT_PARAMETERS_PANEL_LAYOUT.XML";

    private static final String TRUE = "True";

    private static final String FALSE = "False";
    private String allFonts[];
    private String[] fontSizes = { "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36",
	    "48", "72" };
    private String[] alignments = { "Left", "Center", "Right" };
    private final String fontParamatersBorderTitle = "Font Parameters";
    private final int COLOR_TEXT_FIELD_SIZE = 5;
    private final int BORDER_FONT_SIZE = 14;

    private JLabel fontTypeLabel;
    private JLabel fontSizeLabel;
    private JLabel backgroundColorLabel;
    private JLabel foregroundColorLabel;
    private JLabel probePositionsLabel;
    private JLabel horizontalAlignmentLabel;
    private JLabel horizontalPositionLabel;
    private JLabel verticalPositionLabel;

    private JComboBox fontTypeSelector;
    private JComboBox fontSizeSelector;
    private JComboBox horizontalAlignmentSelector;

    private JCheckBox boldFontCheckBox;
    private JCheckBox italicFontCheckBox;

    private JSlider horizontalPositionSlider;
    private JSlider verticalPositionSlider;

    private JTextField backgroundColorTextBox;
    private JTextField foregroundColorTextBox;
    private JTextField horizontalPositionFromLeft;
    private JTextField verticalPositionFromTop;

    private JButton foregroundColorSelectButton;
    private JButton backgroundColorSelectButton;

    private TitledBorder fontParametersPanelBorder;

    private JPanel fontParametersPanel;

    private FontParametersPanelHandler() {
	fontParametersPanel = new JPanel();
	GroupLayout fontParametersPanelLayout = new GroupLayout(fontParametersPanel);
	fontParametersPanel.setLayout(fontParametersPanelLayout);
	TitledBorderFactory titledBorderFactory = new TitledBorderFactory(Color.GRAY, "sansserif", Font.BOLD
		+ Font.ITALIC, BORDER_FONT_SIZE);
	fontParametersPanelBorder = titledBorderFactory.getTitledBorder(fontParamatersBorderTitle);
	fontParametersPanel.setBorder(fontParametersPanelBorder);

	GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	allFonts = ge.getAvailableFontFamilyNames();

	// initialize components to be added
	initComponents(allFonts, alignments, fontSizes);
	addComponents(fontParametersPanelLayout);

    }

    public static FontParametersPanelHandler getInstance() {
	return fontParametersPanelHandlerObject;
    }

    private void initComponents(String[] allFonts, String[] alignments, String[] fontSizes) {
	fontTypeLabel = new JLabel("Font Type:");
	fontSizeLabel = new JLabel("Font Size:");
	backgroundColorLabel = new JLabel("Background Color:");
	foregroundColorLabel = new JLabel("Foreground Color:");
	probePositionsLabel = new JLabel("Probe Positions");
	probePositionsLabel.setFont(new Font("Dialogue", Font.BOLD, 14));
	horizontalAlignmentLabel = new JLabel("Horizontal Alignment");
	horizontalPositionLabel = new JLabel("Horizontal Position");
	verticalPositionLabel = new JLabel("Vertical Position");

	fontTypeSelector = new JComboBox(allFonts);
	horizontalAlignmentSelector = new JComboBox(alignments);
	fontTypeSelector.setSelectedItem("Arial");
	fontSizeSelector = new JComboBox(fontSizes);
	italicFontCheckBox = new JCheckBox("Italic");
	boldFontCheckBox = new JCheckBox("Bold");

	backgroundColorTextBox = new JTextField(COLOR_TEXT_FIELD_SIZE);
	backgroundColorTextBox.setFocusable(false);
	foregroundColorTextBox = new JTextField(COLOR_TEXT_FIELD_SIZE);
	foregroundColorTextBox.setFocusable(false);
	horizontalPositionFromLeft = new JTextField(3);
	verticalPositionFromTop = new JTextField(3);
	backgroundColorTextBox.setEditable(false);
	foregroundColorTextBox.setEditable(false);
	foregroundColorTextBox.setBackground(Color.BLACK);
	horizontalPositionFromLeft.setText("33");
	verticalPositionFromTop.setText("45");

	foregroundColorSelectButton = new JButton("Select");
	foregroundColorSelectButton.addActionListener(this);

	backgroundColorSelectButton = new JButton("Select");
	backgroundColorSelectButton.addActionListener(this);

	horizontalPositionSlider = new JSlider();
	verticalPositionSlider = new JSlider();

    }

    private void addComponents(GroupLayout fontParametersPanelLayout) {
	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(FONT_PARAMETERS_PANEL_LAYOUT_XML_FILE));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	fontParametersPanel.setLayout(panelLayout);

	fontParametersPanel.add(horizontalPositionLabel, "horizontalPositionLabel");
	fontParametersPanel.add(horizontalAlignmentLabel, "horizontalAlignmentLabel");
	fontParametersPanel.add(fontTypeLabel, "fontTypeLabel");
	fontParametersPanel.add(verticalPositionLabel, "verticalPositionLabel");
	fontParametersPanel.add(backgroundColorSelectButton, "backgroundColorSelectButton");
	fontParametersPanel.add(italicFontCheckBox, "italicFontCheckBox");
	fontParametersPanel.add(horizontalAlignmentSelector, "horizontalAlignmentSelector");
	fontParametersPanel.add(foregroundColorTextBox, "foregroundColorTextBox");
	fontParametersPanel.add(foregroundColorLabel, "foregroundColorLabel");
	fontParametersPanel.add(backgroundColorLabel, "backgroundColorLabel");
	fontParametersPanel.add(foregroundColorSelectButton, "foregroundColorSelectButton");
	fontParametersPanel.add(probePositionsLabel, "probePositionsLabel");
	fontParametersPanel.add(verticalPositionSlider, "verticalPositionSlider");
	fontParametersPanel.add(fontSizeLabel, "fontSizeLabel");
	fontParametersPanel.add(horizontalPositionSlider, "horizontalPositionSlider");
	fontParametersPanel.add(backgroundColorTextBox, "backgroundColorTextBox");
	fontParametersPanel.add(fontSizeSelector, "fontSizeSelector");
	fontParametersPanel.add(fontTypeSelector, "fontTypeSelector");
	fontParametersPanel.add(boldFontCheckBox, "boldFontCheckBox");

	// control configuration
	verticalPositionSlider.setMajorTickSpacing(10);
	verticalPositionSlider.setMinorTickSpacing(1);
	verticalPositionSlider.setPaintTicks(true);
	verticalPositionSlider.setPaintLabels(true);
	horizontalPositionSlider.setMajorTickSpacing(10);
	horizontalPositionSlider.setMinorTickSpacing(1);
	horizontalPositionSlider.setPaintTicks(true);
	horizontalPositionSlider.setPaintLabels(true);

	verticalPositionSlider.addChangeListener(new SliderChangeListener());
	horizontalPositionSlider.addChangeListener(new SliderChangeListener());
	horizontalPositionSlider.setToolTipText(Integer.toString(horizontalPositionSlider.getValue()));
	verticalPositionSlider.setToolTipText(Integer.toString(horizontalPositionSlider.getValue()));

    }

    @Override
    public JPanel getPanel() {
	return fontParametersPanel;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
	Object source = event.getSource();

	if (source.equals(foregroundColorSelectButton)) {
	    // System.out.println("Foreground color Select button pressed");
	    Color foregroundColor = JColorChooser.showDialog(this, "Choose Foreground Color", Color.WHITE);
	    foregroundColorTextBox.setBackground(foregroundColor);
	} else if (source.equals(backgroundColorSelectButton)) {
	    // System.out.println("Background color Select button pressed");
	    Color backgroundColor = JColorChooser.showDialog(this, "Choose Background Color", Color.WHITE);
	    backgroundColorTextBox.setBackground(backgroundColor);
	}

    }

    /**
     * get the selected font size on this panel
     * */
    private String getFontSize() {
	return (String) this.fontSizeSelector.getSelectedItem();
    }

    /**
     * get the selected font type on this panel
     * */
    private String getFontType() {
	return (String) this.fontTypeSelector.getSelectedItem();
    }

    /**
     * check is font is bold
     * */
    private String isBold() {
	return ((this.boldFontCheckBox.isSelected()) ? TRUE : FALSE);
    }

    /**
     * check is font is italic
     * */
    private String isItalic() {
	return ((this.italicFontCheckBox.isSelected()) ? TRUE : FALSE);
    }

    /**
     * get the text field color value
     **/
    private int getSelectedColor(JTextField field) {
	Color foregroudColor = field.getBackground();
	String rgb = Integer.toHexString(foregroudColor.getRGB());
	rgb = rgb.substring(2, rgb.length());
	return Integer.parseInt(rgb, 16);
    }

    /**
     * get horizontal alignment
     * */
    private int getHorizontalAlignment() {
	return this.horizontalAlignmentSelector.getSelectedIndex();
    }

    /**
     * get horizontal probe position
     * */
    private int getHorizontalProbePosition() {
	return this.horizontalPositionSlider.getValue();
    }

    /**
     * get vertical probe position
     * */
    private int getVerticalProbePosition() {
	return this.verticalPositionSlider.getValue();
    }

    public String getFontParametersString() {
	// no need for validation because all fields already contain the deault values on this panel
	StringBuffer fontParameters = new StringBuffer();
	fontParameters.append(getFontType());
	fontParameters.append(UtilityConstants.VALUES_DELIMITER);

	fontParameters.append(getFontSize());
	fontParameters.append(UtilityConstants.VALUES_DELIMITER);

	fontParameters.append(isBold());
	fontParameters.append(UtilityConstants.VALUES_DELIMITER);

	fontParameters.append(isItalic());
	fontParameters.append(UtilityConstants.VALUES_DELIMITER);

	fontParameters.append(getSelectedColor(foregroundColorTextBox));
	fontParameters.append(UtilityConstants.VALUES_DELIMITER);

	fontParameters.append(getSelectedColor(backgroundColorTextBox));
	fontParameters.append(UtilityConstants.VALUES_DELIMITER);

	fontParameters.append(getHorizontalAlignment());
	fontParameters.append(UtilityConstants.VALUES_DELIMITER);

	fontParameters.append(getHorizontalProbePosition());
	fontParameters.append(UtilityConstants.VALUES_DELIMITER);

	fontParameters.append(getVerticalProbePosition());

	return fontParameters.toString();

    }
}
